﻿using System;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace DdReader
{
    public partial class frmConverter : Form
    {
        MeasureData md = new MeasureData();

        public frmConverter()
        {
            InitializeComponent();
        }

        private void frmdReader_Load(object sender, EventArgs e)
        {
            this.MaximumSize = this.Size;
            this.MinimumSize = this.Size;

            try
            {
                md.Init();
            }
            catch (Exception ex)
            {
                Log.WriteErrorLog(ex.Message);
                MessageBox.Show(ex.Message);
            }

            toolStripStatusLabel.Text += md.Iicount.ToString();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog od = new FolderBrowserDialog();
            od.RootFolder = System.Environment.SpecialFolder.MyComputer;
            //TODO: ini
            od.SelectedPath = @"E:\For_all\SEANS\SEANS";
            if (od.ShowDialog() == DialogResult.OK)
            {
                txtFileName.Text = od.SelectedPath;
                md.Dirname = txtFileName.Text;
                if (String.IsNullOrEmpty(md.Dirname))
                {
                    btnImport.Enabled = false;
                }
                else
                {
                    btnImport.Enabled = true;
                } 
            }  
        }
        
        public void ProcessDir(string sourceDir, int recursionLvl)
        {
            if (recursionLvl <= MeasureData.HowDeepToScan)
            {
                // Process the list of files found in the directory.
                string[] fileEntries = Directory.GetFiles(sourceDir);
                foreach (string fileName in fileEntries)
                {
                    // do something with fileName
                    md.ProccesFile(fileName, backgroundWorker);
                    //Console.WriteLine(fileName);
                }

                // Recurse into subdirectories of this directory.
                string[] subdirEntries = Directory.GetDirectories(sourceDir);
                foreach (string subdir in subdirEntries)
                    // Do not iterate through reparse points
                    if ((File.GetAttributes(subdir) &
                         FileAttributes.ReparsePoint) !=
                             FileAttributes.ReparsePoint)

                        ProcessDir(subdir, recursionLvl + 1);
            }
        }

        private void frmConverter_FormClosed(object sender, FormClosedEventArgs e)
        {
            md.Conn.Close();
        }

        private void backgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
              backgroundWorker.ReportProgress(0, "Работает...");

                // Wait 100 milliseconds.
              ProcessDir(md.Dirname, MeasureData.HowDeepToScan);
                // Report progress.
                backgroundWorker.ReportProgress(100, "Завершено...");
        }

        private void backgroundWorker_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
        {
            toolStripProgressBar.Value = e.ProgressPercentage;
            toolStripStatusLabel.Text = String.Format("Импортировано записей: {0}", md.Iicount);
            // Set the text.
            //this.Text = e.ProgressPercentage.ToString();
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            backgroundWorker.RunWorkerAsync();
        }
    }
}
